package org.malred.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.malred.hospital.common.R;

import org.malred.hospital.dto.OrderListDto;
import org.malred.hospital.entity.*;
import org.malred.hospital.entity.TbStudent;
import org.malred.hospital.entity.TbStudent;
import org.malred.hospital.service.DoctorService;
import org.malred.hospital.service.OrderService;
import org.malred.hospital.service.StudentService;
import org.malred.hospital.service.StudentService;
import org.malred.hospital.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

//@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping("/student")
public class StudentController extends BaseController<TbStudent> {
    @Autowired
    private StudentService service;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DoctorService doctorService;

    @Override
    public R<List<TbStudent>> findAll() {
        try {
            return R.success(service.list());
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/page")
    public R<List<TbStudent>> page(int page, int size) {
        //分页构造器
        Page<TbStudent> pageInfo = new Page<>(page, size);
        //条件构造器
        LambdaQueryWrapper<TbStudent> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByAsc(TbStudent::getCreateTime);

        //分页查询
        service.page(pageInfo, queryWrapper);

        return R.success(pageInfo.getRecords());
    }

    @GetMapping("/count")
    public R<Integer> count() {
        return R.success(service.count());
    }

    @Override
    public R<TbStudent> findById(@PathVariable Long id) {
        return R.success(service.getById(id));
    }

    @GetMapping("/find/name/{name}")
    public R<List<TbStudent>> findByName(@PathVariable String name) {
        LambdaQueryWrapper<TbStudent> qw = new LambdaQueryWrapper<>();
        qw.like(TbStudent::getName, name);
        List<TbStudent> list = service.list(qw);
        if (list != null) {
            return R.success(list);
        }
        return R.error("[Student] 未找到");
    }

    /**
     * 预订医生
     *
     * @param sid
     * @param did
     * @return
     */
    @PostMapping("/order/{sid}/{did}")
    public R<String> order(@PathVariable Long sid,@PathVariable Long did) {
        if (doctorService.getById(did) == null) {
            return R.error("没有该医生!");
        }
        if (service.getById(sid) == null) {
            return R.error("没有该学生!");
        }
        TbOrder order = new TbOrder();
        order.setStudentId(sid);
        order.setDoctorId(did);
        order.setStatus(0L); // 未就诊

        boolean save = orderService.save(order);
        if (!save) {
            return R.error("预订失败!");
        }
        return R.success("预订成功!");
    }

    @Override
    public R<TbStudent> insert(@RequestBody TbStudent dto) {
        dto.setNumber(0L);
        if (service.save(dto)) {
            return R.success(dto);
        } else {
            return R.error("添加 [Student] 失败");
        }
    }

    @Override
    public R<TbStudent> update(@RequestBody TbStudent dto) {
        if (service.saveOrUpdate(dto)) {
            return R.success(dto);
        } else {
            return R.error("修改 [Student] 失败");
        }
    }

    @Override
    public R<TbStudent> delete(@PathVariable Long id) {
        if (service.removeById(id)) {
            return R.success(null);
        } else {
            return R.error("删除 [Student] 失败");
        }
    }
}
